//
//  MKCustomAnimateSelectView.m
//  MKScrollSelectDemo
//
//  Created by moyekong on 15/4/2.
//  Copyright (c) 2015年 17house. All rights reserved.
//

#import "MKCustomAnimateSelectView.h"
#import "MKCustomAsSegmentView.h"
#import "MKOrderView.h"

@interface MKCustomAnimateSelectView ()<UIScrollViewDelegate, MKCustomAsSegmentViewDelegate>

@property (nonatomic, strong) UIScrollView * scorllView;
@property (nonatomic, strong) MKCustomAsSegmentView * customAsSegmentView;

@property (nonatomic, assign) NSInteger selectViewNum;

@property (weak, nonatomic) IBOutlet UIView *topBackView;
@property (weak, nonatomic) IBOutlet UIView *bottomBackView;


@end

@implementation MKCustomAnimateSelectView
// 设备宽、高
#define deviceWidth ([UIScreen mainScreen].bounds.size.width)
#define deviceHeight ([UIScreen mainScreen].bounds.size.height)

- (instancetype)initWithFrame:(CGRect)frame andDataArray:(NSMutableArray *)dataArray
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"MKCustomAnimateSelectView" owner:self options:nil] lastObject];
    self.frame = frame;
    if (self) {
        self.dataArray = [NSMutableArray arrayWithArray:dataArray];
        self.selectViewNum = 0;
        [self _setupSubviews];
    }
    return self;
}

- (void)_setupSubviews {
    
    [self _setupScrollSubview];
    
}

- (void)_setupScrollSubview {
    
    NSMutableArray * nameArray = [NSMutableArray arrayWithArray:_dataArray];
    self.customAsSegmentView = [[MKCustomAsSegmentView alloc] initWithFrame:CGRectMake(0.0, 0.0, deviceWidth, 38.0) andSegmentDataArray:nameArray];
    self.customAsSegmentView.delete = self;
    [self.topBackView addSubview:self.customAsSegmentView];
    
    CGFloat orderViewH = deviceHeight - 38.0 - 64.0; // 38.0是segmentView 高度
    self.scorllView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, deviceWidth, orderViewH)];
    [self.bottomBackView addSubview:self.scorllView];
    self.scorllView.delegate = self;
    self.scorllView.pagingEnabled = YES;
    self.scorllView.bounces = NO;
    self.scorllView.showsHorizontalScrollIndicator = NO;
    for (int i = 0; i < nameArray.count; i++) {
        MKOrderView * tempOrderView = [[MKOrderView alloc] initWithFrame:CGRectMake(i * deviceWidth, 0.0, deviceWidth, orderViewH)];
        [self.scorllView addSubview:tempOrderView];
        tempOrderView.tag = 03131615 + i;
        switch (i) {
            case 0:
                tempOrderView.backgroundColor = [UIColor lightGrayColor];
                tempOrderView.showLabel.text = @"first View";
                break;
            case 1:
                tempOrderView.backgroundColor = [UIColor orangeColor];
                tempOrderView.showLabel.text = @"second View";
                break;
            case 2:
                tempOrderView.backgroundColor = [UIColor greenColor];
                tempOrderView.showLabel.text = @"third View";
                break;
            case 3:
                tempOrderView.backgroundColor = [UIColor grayColor];
                tempOrderView.showLabel.text = @"fourth View";
                break;
            default:
                break;
        }
    }
    [self.scorllView setContentSize:CGSizeMake(nameArray.count * deviceWidth, orderViewH)];
    [self.scorllView setContentOffset:CGPointMake(0, 0)];
}

#pragma mark - 点击头部button的响应方法 -
- (void)clickedCustomSegmentViewButton:(NSInteger)btnNum
{
    self.selectViewNum = btnNum;
    [self.customAsSegmentView animateSelectedViewWithNum:btnNum];
    CGFloat offsetX = btnNum * deviceWidth;
    [self.scorllView setContentOffset:CGPointMake(offsetX, 0.0)];
}

#pragma mark - scrollView delegate -
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat scrollViewW = scrollView.frame.size.width;
    CGFloat offsetX = scrollView.contentOffset.x;
    int num = (offsetX + scrollViewW / 2) / scrollViewW;
    [self.customAsSegmentView animateSelectedViewWithNum:num];
}

#pragma mark - 获取当前显示的view -
- (MKOrderView *)getCurrentShowView {
    
    for (id object in self.scorllView.subviews) {
        if ([object isKindOfClass:[MKOrderView class]]) {
            MKOrderView * targetOrderView = (MKOrderView *)object;
            if (targetOrderView.tag == 03131615 + _selectViewNum) {
                return targetOrderView;
            }
        }
    }
    return nil;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
